package net.lcheng.manage.controller;

import com.alibaba.druid.util.StringUtils;
import net.lcheng.commons.utils.PageList;
import net.lcheng.manage.utils.ControllerUtils;
import net.lcheng.manage.vo.PCategoryModel;
import net.lcheng.model.PCategory;
import net.lcheng.service.CategoryService;
import net.lcheng.service.SysPermissionsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by zwr on 2016/6/27 0027.
 */
@Controller
public class PCategoryController {
    @Autowired
    private CategoryService pcategoryService;
    @Autowired
    private SysPermissionsService permissionsService;
    /**
     * 分类管理
     */
    @RequiresPermissions("platform:category_list")
    @RequestMapping(value = "/pcategory/category_list", method = RequestMethod.GET)
    public String list(Model model) {
        ControllerUtils.common(model, permissionsService, "platform:category_list");
        PageList<PCategory> pCategoryPageList = pcategoryService.getCategoryByStatus(1, 100);
        model.addAttribute("course", pCategoryPageList.list);
        return "pcategory/category_list";
    }

    /**
     * 添加分类
     */
    @RequiresPermissions("platform:category_add")
    @RequestMapping(value = "/pcategory/category_add", method = RequestMethod.GET)
    public String categoryGet(Model model) {
        ControllerUtils.common(model, permissionsService, "platform:category_add");
        if (!model.containsAttribute("PCategoryModel")) {
            model.addAttribute("PCategoryModel", new PCategoryModel());
        }
        /**获取一级分类*/
        List<PCategory> topCategoryList = pcategoryService.getChildCategoryByParentCategoryId("");
        model.addAttribute("topCategoryList", topCategoryList);
        return "pcategory/category_add";
    }

    /**
     * 添加分类Post
     */
    @RequiresPermissions("platform:category_add")
    @RequestMapping(value = "/pcategory/category_add", method = RequestMethod.POST)
    public String permissionsEditPost(Model model, @Valid @ModelAttribute("PCategoryModel") PCategoryModel pCategoryModel, BindingResult result) {
        if (result.hasErrors()) {
            return "pcategory/category_add";
        }
        int returnResult = pcategoryService.insert(pCategoryModel.getCategoryName(), pCategoryModel.getCategoryImage(), pCategoryModel.getTopCategoryId(), pCategoryModel.getSecondCategoryId(), pCategoryModel.getOrderNum(), pCategoryModel.getSummary());
        if (returnResult > 0) {
            model.addAttribute("error", "修改成功");
        } else model.addAttribute("error", "系统异常");
        return "redirect:/pcategory/category_list";
    }

    /**
     * 分类修改Get
     */
    @RequiresPermissions("platform:category_modify")
    @RequestMapping(value = "/pcategory/category_modify", method = RequestMethod.GET)
    public String categoryModify(Model model, @RequestParam String id) {
        ControllerUtils.common(model, permissionsService, "platform:category_modify");
        if (StringUtils.isEmpty(id)) return "error";

        PCategory pCategory = pcategoryService.getByCategoryId(id);
        model.addAttribute("pCategory", pCategory);
        /**获取一级分类*/
        List<PCategory> topCategoryList = pcategoryService.getChildCategoryByParentCategoryId("");
        model.addAttribute("topCategoryList", topCategoryList);
        /**如果是三级分类，获取二级分类*/
        if (pCategory.getDepth() == 3) {
            List<PCategory> secondCategoryList = pcategoryService.getChildCategoryByParentCategoryId(pCategory.getSecondCategoryId());
            model.addAttribute("secondCategoryList", secondCategoryList);
        } else {
            model.addAttribute("secondCategoryList", null);
        }
        PCategoryModel categoryModel = new PCategoryModel();
        categoryModel.setTopCategoryId(pCategory.getTopCategoryId());
        categoryModel.setSecondCategoryId(pCategory.getSecondCategoryId());
        categoryModel.setCategoryId(pCategory.getCategoryId());
        categoryModel.setCategoryName(pCategory.getCategoryName());
        categoryModel.setOrderNum(pCategory.getOrderNum());
        model.addAttribute("PCategoryModel", categoryModel);

        return "pcategory/category_modify";
    }

    /**
     * 分类修改post
     */
    @RequiresPermissions("platform:category_modify")
    @RequestMapping(value = "/pcategory/category_modify", method = RequestMethod.POST)
    public String categoryModifyPost(Model model, @Valid @ModelAttribute("PCategoryModel") PCategoryModel pCategoryModel, BindingResult result) {
        ControllerUtils.common(model, permissionsService, "platform:category_modify");
        if (result.hasErrors()) {
            return "/pcategory/category_modify";
        }
        int returnResult = pcategoryService.modify(pCategoryModel.getCategoryId(), pCategoryModel.getCategoryName(), pCategoryModel.getCategoryImage(), pCategoryModel.getTopCategoryId(), pCategoryModel.getOrderNum(), pCategoryModel.getSecondCategoryId(), pCategoryModel.getSummary());

        if (returnResult > 0) {
            model.addAttribute("error", "修改成功");
        } else model.addAttribute("error", "系统异常");
        return "redirect:/pcategory/category_list";
    }

    /**
     * 删除Get
     */
    @RequiresPermissions("platform:category_delete")
    @RequestMapping(value = "/pcategory/category_delete", method = RequestMethod.GET)
    public String categoryDeleteGet(Model model, @RequestParam String id) {
        int returnResult = pcategoryService.updateStatus(id, 2);
        if (returnResult > 0) {
            model.addAttribute("error", "修改成功");
        } else model.addAttribute("error", "系统异常");
        return "redirect:/pcategory/category_list";
    }

    /***
     * 根据父类id获取子类
     *
     * @param parentCategoryId 父类id
     * @return
     */
    @RequestMapping(value = "/get_child_category/{parentCategoryId}")
    public
    @ResponseBody
    List<PCategory> getSchoolAjax(@PathVariable("parentCategoryId") String parentCategoryId) {
        if (StringUtils.isEmpty(parentCategoryId) == false) {
            List<PCategory> categoryList = pcategoryService.getCategoryId(parentCategoryId);
            return categoryList;
        } else return null;
    }
}